#ifndef _BSP_I2C_H
#define _BSP_I2C_H


typedef const int soft_iic_dev;

struct soft_iic_config {
    int scl;
    int sda;
    u32 delay;
    u8 io_pu;
};
u8 soft_iic_tx_byte(soft_iic_dev iic, u8 byte);
u8 soft_iic_rx_byte(soft_iic_dev iic, u8 ack);
void soft_iic_stop(soft_iic_dev iic);
int soft_iic_init(soft_iic_dev iic);
void soft_iic_uninit(soft_iic_dev iic);
void soft_iic_start(soft_iic_dev iic);
int soft_iic_read_buf(soft_iic_dev iic, void *buf, int len);
int soft_iic_write_buf(soft_iic_dev iic, const void *buf, int len);
void soft_iic_suspend(soft_iic_dev iic);
void soft_iic_resume(soft_iic_dev iic);

// #if 1
// const struct soft_iic_config soft_iic_cfg[] = {
//     //iic0 data
//     {
//         .scl = TCFG_SW_I2C0_CLK_PORT,                   //IIC CLK脚
//         .sda = TCFG_SW_I2C0_DAT_PORT,                   //IIC DAT脚
//         .delay = TCFG_SW_I2C0_DELAY_CNT,                //软件IIC延时参数，影响通讯时钟频率
//         .io_pu = 1,                                     //是否打开上拉电阻，如果外部电路没有焊接上拉电阻需要置1
//     },
// #if TCFG_PAY_ALIOS_ENABLE && (TCFG_PAY_ALIOS_WAY_SEL==TCFG_PAY_ALIOS_WAY_T_HEAD)
//     //iic1 data
//     {
//         .scl = IO_PORTG_02,
//         .sda = IO_PORTG_00,
//         .delay = 75,
//         .io_pu = 1,
//     },
// #endif
// };

#if I2C_SW_EN
void bsp_i2c_start(void);
void bsp_i2c_stop(void);
void bsp_i2c_tx_byte(uint8_t dat);
uint8_t bsp_i2c_rx_byte(void);
bool bsp_i2c_rx_ack(void);
void bsp_i2c_tx_ack(void);
void bsp_i2c_tx_nack(void);
#elif I2C_HW_EN
#define DATA_CNT_1B     1
#define DATA_CNT_2B     2
#define DATA_CNT_3B     3
#define DATA_CNT_4B     4

#define START_FLAG0     BIT(3)
#define DEV_ADDR0       BIT(4)
#define REG_ADDR_0      BIT(5)
#define REG_ADDR_1      (BIT(5) | BIT(6))

#define START_FLAG1     BIT(7)
#define DEV_ADDR1       BIT(8)
#define RDATA           BIT(9)
#define WDATA           BIT(10)

#define STOP_FLAG       BIT(11)
#define NACK            BIT(12)

//i2c_cfg配置I2C数据格式：
//1、发送：[START_FLAG0]  [DEV_ADDR0] [REG_ADDR_0(REG_ADDR_1, 16位地址)] [WDATA] [STOP_FLAG] （设上WDATA）
//2、接收：[START_FLAG0]  [DEV_ADDR0] [REG_ADDR_0(REG_ADDR_1, 16位地址)] [START_FLAG1] [DEV_ADDR1] [RDATA] [STOP_FLAG]（设上RDATA）

void bsp_i2c_tx_byte(u16 dev_addr, u16 reg_addr, u32 data);
void bsp_i2c_rx_buf(u16 dev_addr, u16 reg_addr, u8 *buf, u16 len);
void bsp_i2c_init(void);
#endif
#endif
